/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"github.com/pkg/errors"
	"github.com/rs/zerolog/log"
	"github.com/spf13/viper"
	"kvmc/app"

	"github.com/spf13/cobra"
)

// stopCmd represents the stop command
var stopCmd = &cobra.Command{
	Use:   "stop",
	Short: "停止node",
	Long:  ``,
	Args: func(cmd *cobra.Command, args []string) error {
		if stopOpt.Name == "" && len(args) > 0 {
			stopOpt.Name = args[0]
		}
		if stopOpt.Name == "" {
			return errors.New("node名称不能为空")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		manager, err := app.NewManager(viper.GetString("configPath"))
		if err != nil {
			log.Err(err).Send()
			return
		}
		manager.Stop(stopOpt)
	},
}

var stopOpt = new(app.StopOption)

func init() {
	rootCmd.AddCommand(stopCmd)

	stopCmd.Flags().StringVarP(&stopOpt.ClusterName, "cluster", "c", "", "集群名称")
	stopCmd.Flags().StringVarP(&stopOpt.Name, "name", "n", "", "node的名称")
	stopCmd.Flags().BoolVarP(&stopOpt.All, "all", "A", false, "停止集群内所有node")
	stopCmd.Flags().BoolVarP(&stopOpt.Force, "force", "f", false, "强制停止(断电)")
}
